package test00.tx05;

import java.lang.annotation.*;
import java.lang.reflect.Field;

// 定义健康状态枚举
enum HealthStatus {
    EXCELLENT, GOOD, FAIR, POOR
}

// 定义睡眠状态枚举
enum SleepStatus {
    HEALTHY, INSOMNIA
}

// 健康状态注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface HealthCondition {
}

// 睡眠状态注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface SleepCondition {
}

// 动物抽象类
abstract class Animal {
    @HealthCondition
    protected HealthStatus healthStatus;

    @SleepCondition
    protected SleepStatus sleepStatus;

    public abstract void eat();

    public void sleep() {
        System.out.println(this.getClass().getSimpleName() + " 正在睡觉");
    }
}

// 哺乳动物抽象类
abstract class Mammal extends Animal { }

// 海洋动物抽象类
abstract class MarineAnimal extends Animal { }

// 飞禽动物抽象类
abstract class Bird extends Animal { }

// 具体动物实现
class Lion extends Mammal {
    @Override
    public void eat() {
        System.out.println("狮子在吃肉");
    }
}

class Tiger extends Mammal {
    @Override
    public void eat() {
        System.out.println("老虎在吃肉");
    }
}

class Shark extends MarineAnimal {
    @Override
    public void eat() {
        System.out.println("鲨鱼在吃鱼");
    }
}

class Dolphin extends MarineAnimal {
    @Override
    public void eat() {
        System.out.println("海豚在吃小鱼");
    }
}

// 饲养员基类（使用泛型限定动物类型）
abstract class Keeper<T extends Animal> {
    public abstract void feed(T animal);

    public void inspect(T animal) {
        try {
            // 通过反射检查睡眠状态
            Field[] fields = animal.getClass().getSuperclass().getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(SleepCondition.class)) {
                    field.setAccessible(true);
                    SleepStatus status = (SleepStatus) field.get(animal);
                    if (status == SleepStatus.INSOMNIA) {
                        System.out.println("为 " + animal.getClass().getSimpleName() + " 分发安眠药");
                        // 更新睡眠状态
                        field.set(animal, SleepStatus.HEALTHY);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void checkHealth(T animal, HealthStatus status) {
        try {
            // 通过反射设置健康状态
            Field[] fields = animal.getClass().getSuperclass().getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(HealthCondition.class)) {
                    field.setAccessible(true);
                    field.set(animal, status);
                    System.out.println(animal.getClass().getSimpleName() + " 健康状态设置为: " + status);
                    if (status == HealthStatus.POOR) {
                        System.out.println("需要特别照看 " + animal.getClass().getSimpleName());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// 具体饲养员实现
class MammalKeeper extends Keeper<Mammal> {
    @Override
    public void feed(Mammal animal) {
        animal.eat();
    }
}

class MarineKeeper extends Keeper<MarineAnimal> {
    @Override
    public void feed(MarineAnimal animal) {
        animal.eat();
    }
}

// 测试类
public class ZooSystem {
    public static void main(String[] args) {
        // 创建动物实例
        Mammal lion = new Lion();
        MarineAnimal shark = new Shark();

        // 设置初始状态
        try {
            Field sleepField = Animal.class.getDeclaredField("sleepStatus");
            sleepField.setAccessible(true);
            sleepField.set(lion, SleepStatus.INSOMNIA);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建饲养员
        MammalKeeper mammalKeeper = new MammalKeeper();
        MarineKeeper marineKeeper = new MarineKeeper();

        // 喂养测试
        mammalKeeper.feed(lion);  // 输出: 狮子在吃肉

        // 巡查测试
        mammalKeeper.inspect(lion);  // 输出: 为 Lion 分发安眠药

        // 健康检查测试
        mammalKeeper.checkHealth(lion, HealthStatus.POOR);
        // 输出: Lion 健康状态设置为: POOR
        // 输出: 需要特别照看 Lion

        // 海洋动物测试
        marineKeeper.feed(shark);  // 输出: 鲨鱼在吃鱼
    }
}